<?php
/**
 * Main window class for PhpDocGtk
 * 
 * @author hcderaad
 * @package PhpGtkDoc
 */
class PhpGtkDoc_Window extends GtkWindow {
	
	/**
	 * The main VBox.
	 * 
	 * @var GtkVBox
	 */
	protected $_mainVBox;
	
	/**
	 * The main menu.
	 * 
	 * @var GtkMenuBar
	 */
	protected $_mainMenu = null;
	
	/**
	 * The main window.
	 * 
	 * @var GtkContainer
	 */
	protected $_mainView = null;
	
	/**
	 * The main statusbar;
	 * 
	 * @var GtkStatusbar
	 */
	protected $_mainStatus;
	
	/**
	 * The most recents message its contextid.
	 * 
	 * @var int
	 */
	protected $_mainStatusLastContextId;
	
	/**
	 * Constructor
	 */
	public function __construct() {
		parent::__construct();
		$this->connect("destroy",array($this,'closeWindow'));
		$this->set_title(PhpDocGtk::t('PhpDocGtk'));
		$this->_mainVBox = new GtkVBox();
		$this->_mainView = new GtkBin();
	}
	
	/**
	 * Sets the title (PhpDocGtk - title)
	 * 
	 * @param string $title
	 * @return PhpDocGtk_Window
	 */
	public function setTitle($title) {
		$this->set_title(PhpDocGtk::t('PhpDocGtk') . ' - ' . $title);
		return $this;
	}
	
	/**
	 * Returns the main VBox
	 * 
	 * @return GtkVBox
	 */
	public function getMainBox() {
		return $this->_mainVBox;
	}
	
	/**
	 * Returns the MenuBar
	 * 
	 * @return GtkMenuBar
	 */
	public function getMenu() {
		if(empty($this->_mainMenu) || !$this->_mainMenu instanceof GtkMenuBar) {
			$this->_mainMenu = new GtkMenuBar();
		}
		return $this->_mainMenu;
	}
	
	/**
	 * Sets the menu
	 * 
	 * @param GtkMenuBar $menu
	 * @return GtkMenuBar
	 */
	public function setMenu(GtkMenuBar $menu) {
		$this->_mainMenu = $menu;
		return $this->_mainMenu;
	}
	
	/**
	 * Returns the main container, if none set a GtkBin object is returned.
	 * 
	 * @return GtkContainer
	 */
	public function getMainView() {
		return $this->_mainView;
	}
	
	/**
	 * Sets the main view
	 * 
	 * @param GtkContainer $view
	 * @return GtkContainer
	 */
	public function setMainView(GtkContainer $view) {
		$this->_mainView = $view;
		return $this->_mainView;
	}

	/**
	 * Sets the main statusbar
	 * 
	 * @return GtkStatusbar
	 */
	public function setStatusBar(GtkStatusbar $status) {
		$this->_mainStatus = $status;
		return $this->_mainStatus;
	}
	
	/**
	 * Returns the statusbar.
	 * 
	 * @return GtkStatusbar
	 */
	public function getStatusBar() {
		if(empty($this->_mainStatus) || !$this->_mainStatus instanceof GtkStatusbar) {
			$this->_mainStatus = new GtkStatusbar();
		}
		return $this->_mainStatus;
	}
	
	/**
	 * Sets the statusmessage
	 * 
	 * @param string $message
	 * @return PhpDocGtk_Window
	 */
	public function setStatusText($message = null) {
		if(!empty($this->_mainStatusLastContextId)) {
			$this->_mainStatus->pop($this->_mainStatusLastContextId);
		}
		$this->_mainStatusLastContextId = $this->_mainStatus->push($message);
		return $this;
		
	}

	/**
	 * Closes the window
	 * 
	 * @return void
	 */
	public function closeWindow() {
		return Gtk::main_quit();
	}
	
	/**
	 * Renders and shows the window
	 * 
	 * @return PhpDocGtk_Window
	 */
	public function render() {
		if(!empty($this->_mainMenu) && $this->_mainMenu instanceof GtkMenuBar) {
			$this->_mainVBox->pack_start($this->_mainMenu,0,0);
		}
		$this->_mainVBox->pack_start($this->_mainView);
		if(!empty($this->_mainStatus) && $this->_mainStatus instanceof GtkStatusbar) {
			$this->_mainVBox->pack_start($this->_mainStatus,0,0);
		}
		$this->_mainVBox->show_all();
		$this->show_all();
		return $this;
	}
}
?>